package com.qiangesoft.blog.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.system.entity.SysRole;
import com.qiangesoft.blog.system.entity.SysUserRole;
import com.qiangesoft.blog.system.mapper.SysRoleMapper;
import com.qiangesoft.blog.system.service.ISysRoleService;
import com.qiangesoft.blog.system.service.ISysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色  服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-01
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysUserRoleService userRoleService;

    @Override
    public PageResultVO<SysRole> listRole(PageQuery pageQuery, String name) {
        PageResultVO<SysRole> pageResultVO = new PageResultVO<>();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);

        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), SysRole::getName, name);
        Page<SysRole> rolePage = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResultVO.setTotal(rolePage.getTotal());
        pageResultVO.setPages(rolePage.getPages());
        pageResultVO.setResults(rolePage.getRecords());
        return pageResultVO;
    }

    @Override
    public List<SysRole> listAllRole(String name) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), SysRole::getName, name);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertRole(SysRole role) {
        baseMapper.insert(role);
        baseMapper.insertBatchByRole(role.getMenus(), role.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(SysRole role) {
        baseMapper.updateById(role);

        //先删所有权限在新增
        baseMapper.delByRoleId(role.getId(), null);
        baseMapper.insertBatchByRole(role.getMenus(), role.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
        ids.forEach(id -> baseMapper.delByRoleId(id, null));
    }

    @Override
    public List<Long> getCurrentUserRole() {
        Long roleId = baseMapper.queryByUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        return baseMapper.queryByRoleMenu(roleId);
    }

    @Override
    public List<Long> selectById(Long roleId) {
        return baseMapper.queryByRoleMenu(roleId);
    }

    @Override
    public List<SysRole> listRoleByUserId(Long userId) {
        LambdaQueryWrapper<SysUserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> userRoleList = userRoleService.list(userRoleWrapper);
        if (CollectionUtils.isEmpty(userRoleList)) {
            return Collections.emptyList();
        }

        Set<Long> roleIds = userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toSet());
        return this.listByIds(roleIds);
    }
}
